<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            background-color: pink;
        }

        .change {
            background-color: purple;
            color: #fff;
            font-size: 25px;
            margin-top: 100px;
        }
    </style>
</head>

<body>
    <div>文本</div>
    <script>
        var test = document.querySelector('div');
        //如果样式多或功能复杂使用此方式比较麻烦
        // test.onclick = function () {
        //     this.style.backgroundColor = 'purple';
        //     this.style.color = '#fff';
        //     this.style.fontSize = '25px';
        //     this.style.marginTop = '100px';
        // }

        //可以直接修改className
        test.onclick = function () {
            this.className = 'change';
        }
    </script>
</body>

</html>